package com.handmark.pulltorefresh.samples;

import java.util.ArrayList;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class TestViewPagerActivity extends Activity{

	ViewPager mViewPager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_fragment);
		mViewPager = (ViewPager)findViewById(R.id.viewPager);
		initView();
	}
	
	private void initView(){
		mViewPager = (ViewPager)findViewById(R.id.viewPager);

		ArrayList<View> layouts = new ArrayList<View>();
		for (int i = 0; i < 5; i++) {
			View layout = createView(i);
			layouts.add(layout);
		}

		HomePagerAdapter pagerAdapter = new HomePagerAdapter(layouts);
		mViewPager.setAdapter(pagerAdapter);
	}


	
	private View createView(int id){
		final PullToRefreshScrollView mPullRefreshScrollView;
		LayoutInflater inflater = LayoutInflater.from(this);
		LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.activity_ptr_scrollview, null);
		mPullRefreshScrollView = (PullToRefreshScrollView)layout.findViewById(R.id.pull_refresh_scrollview);
		mPullRefreshScrollView.setMode(Mode.PULL_FROM_START);
		mPullRefreshScrollView.setOnRefreshListener(new OnRefreshListener<ScrollView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
				//new GetDataTask().execute();
				mPullRefreshScrollView.onRefreshComplete();
			}
		});

		ScrollView mScrollView = mPullRefreshScrollView.getRefreshableView();
		return layout;
	}
	
	private class GetDataTask extends AsyncTask<Void, Void, String[]> {

		@Override
		protected String[] doInBackground(Void... params) {
			// Simulates a background job.
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
			}
			return null;
		}

		@Override
		protected void onPostExecute(String[] result) {
			// Do some stuff here

			// Call onRefreshComplete when the list has been refreshed.
			//mPullRefreshScrollView.onRefreshComplete();

			//super.onPostExecute(result);
		}
	}

	private class HomePagerAdapter extends PagerAdapter{

		private ArrayList<View> lists;

		public HomePagerAdapter(ArrayList<View> lists){
			this.lists = lists;
		}

		@Override
		public int getCount() {
			return lists.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			((ViewPager)container).removeView(lists.get(position));
		}

		@Override
		public Object instantiateItem(View container, int position) {
			((ViewPager)container).addView(lists.get(position));
			return lists.get(position);
		}

	}


}
